#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File        :   226.翻转二叉树.py    
@Contact     :   lhouctcs@163.com
@License     :   MIT
@Time        :   2023/2/15 22:16
@Author      :   lhou 
@Version     :   0.1.0
@Description :
"""
from typing import Optional


# import lib
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        def invert(root, node):
            if not root:
                return
            if root.left:
                node.right = TreeNode(root.left.val)
                invert(root.left, node.right)
            if root.right:
                node.left = TreeNode(root.right.val)
                invert(root.right, node.left)

        if not root:
            return None
        node = TreeNode(root.val)
        invert(root, node)
        return node
